import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../pages/Login'
import Register from '../pages/Register'
import Manage from '../pages/Manage'

import {getFunctions} from '../service/index'
import {getSession} from '../service'

import TestVuex from '../pages/TestVuex'
import store from '../store'
import Student from '../pages/Student'
import Class from '../pages/Class'
import Course from '../pages/Course'
import Total from '../pages/Total'

Vue.use(VueRouter)

const manageRoutes = {path:'/manage',component:Manage}
// children:[
//     {path:'/manage/student',component:Student},
//     {path:'/manage/class',component:Class},
//     {path:'/manage/course',component:Course},
//     {path:'/manage/total',component:Total}
// ]}
const routes = [
    {path:'/testVuex',component:TestVuex},
    {path:'/login',component:Login},
    {path:'/register',component:Register},
    // manageRoutes,
    {path:'/manage',component:Manage,
      
        children:[
            {path:'/manage/student',component:Student},
            {path:'/manage/class',component:Class},
            {path:'/manage/course',component:Course},
            {path:'/manage/total',component:Total}
        ]
    }
]
const router = new VueRouter({
    mode:'history',
    routes,
    isLogin:false
})

// router.beforeEach(async (to, from, next) => {
//     if(to.path == "/login" || to.path == "/register" || to.path == "/testVuex"){
//         next();
//         return;
//     }
//     if(!store.state.user ){
//         let data = await getSession();
//         if(data.data){
//             store.commit("setLoginUser",data.data);
//             generateMenus(data.data.phone);
//             next();
//         }else{
//             next('/login');
//         }
//     }
//     next();

  
    
// })
export const generateMenus = async (phone) => {
    // 根据权限组装路由
    // 获取该用户的所有权限
    let data = await getFunctions(phone);
    store.commit("setFunctions",data);
    let childrenData = []
    // 循环权限数组，组装路由
    for(let func of data){
        childrenData.push({path:func.url,component:() => import('@/pages/'+func.component+'.vue')})
    }
    manageRoutes.children = childrenData;
    router.addRoutes([manageRoutes]);

    
    
}
export default router;